﻿using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;
using System.Reflection;

namespace SDL
{
	public partial class ORM
    {
        private static void FillDataObjectFromReader(DBObject oObj, IDataRecord reader)
        {
            var type = oObj.GetType();
            var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public /*| BindingFlags.DeclaredOnly*/);
            foreach (var prop in props)
            {
                var attrs = prop.GetCustomAttributes(typeof(DBFieldAttribute), false);
                foreach (DBFieldAttribute attr in attrs)
                {
                    try
                    {
                        var name = prop.Name;
                        var value = reader[attr.FieldName];

                        if (value.GetType() == typeof(Guid))
                        {
                            oObj.SetField(name, new object[] { value.ToString() });
                            continue;
                        }
                        if (value.GetType() == typeof(System.Decimal))
                        {
                            oObj.SetField(name, new object[] { Convert.ToDouble(value) });
                            continue;
                        }
                        oObj.SetField(name, new[] { value });
                    }
                    catch (IndexOutOfRangeException) {/* Якщо reader не містить поля класу */}
                }
            }
        }

        private static void FillDataObject(DBObject oObj, IDictionary reader)
        {
            var type = oObj.GetType();
            var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public /*| BindingFlags.DeclaredOnly*/);
            foreach (var prop in props)
            {
                var attrs = prop.GetCustomAttributes(typeof(DBFieldAttribute), false);
                foreach (DBFieldAttribute attr in attrs)
                {
                    try
                    {
                        var name = prop.Name;
                        var value = reader[attr.FieldName];

                        if (value != null)
                        {
                            if (value.GetType() == typeof (Guid))
                            {
                                oObj.SetField(name, new object[] {value.ToString()});
                                continue;
                            }
                            if (value.GetType() == typeof(System.Decimal))
                            {
                                oObj.SetField(name, new object[] { Convert.ToDouble(value) });
                                continue;
                            }
                            oObj.SetField(name, new[] { value });
                        }
                    }
                    catch (IndexOutOfRangeException) {/* Якщо reader не містить поля класу */}
                }
            }
        }

		private static Dictionary<string, object> getFields(DBObject oObj)
		{
			var type = oObj.GetType();
			var dict = new Dictionary<string, object>();

            var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public /*| BindingFlags.DeclaredOnly*/);
            foreach (var prop in props)
			{
				var attrs = prop.GetCustomAttributes(typeof(DBFieldAttribute), true);
				foreach (DBFieldAttribute attr in attrs)
				{
                    var oField = oObj.GetField(prop.Name);
					dict.Add(attr.FieldName, oField);
				}
			}
			return (dict);
		}
	}
}
